package com.changgou.file.util;

import com.changgou.file.FastDFSfile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;

public class FastDFSClien {
    private static final org.csource.fastdfs.ClientGlobal ClientGlobal = null;

    static {
        try {
            //获取tracker的配置文件fdfs_client.conf的位置
            String filePath = new ClassPathResource("fdfs_client.conf").getPath();
            //加载tracker配置信息
            ClientGlobal.init(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static String[] upload(FastDFSfile fastDFSfile) throws Exception{
        TrackerClient trackerClient= new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        StorageClient storageClient = new StorageClient(trackerServer,null);
        NameValuePair[] mete_list = new NameValuePair[]{
                new NameValuePair(fastDFSfile.getAuthor())
        };
        String[] jpgs = storageClient.upload_file(fastDFSfile.getContent(), fastDFSfile.getExt(), mete_list);
        return jpgs;
    }

    public static byte[] downFile(String groupName, String remoteFileName) throws Exception {
        TrackerClient trackerClient = new TrackerClient();
        //4.根据trackerclient获取到链接对象 trackerServer
        TrackerServer trackerServer = trackerClient.getConnection();
        //5.创建storageServer对象 设置null

        //6.创建stroageClient --->提供了很多的操作图片的API的代码（上传图片，下载 ，删除）
        StorageClient storageClient = new StorageClient(trackerServer, null);

        //7.下在图片
        //参数1 指定要下载的组名
        //参数2 指定要下载的远程文件路径
        byte[] group1s = storageClient.download_file(groupName, remoteFileName);
        return group1s;
    }
}
